#!/bin/sh
#|-*- mode:lisp -*-|#
#|
exec ros -Q -- $0 "$@"
|#
(progn ;;init forms
  (ros:ensure-asdf)
  #+quicklisp(ql:quickload '(:lilu-swank/client :lilu-roswell :lilu-debugger/emacs :lilu-launcher :lilu-swank :cffi) :silent t)
  )

(defpackage :ros.script.lilu-laucher.3842071980
  (:use :cl))
(in-package :ros.script.lilu-laucher.3842071980)

(opts:define-opts
  #.lilu-roswell:*default-help-option*
  #.(lilu-roswell:merge-options
     lilu-roswell:*default-swank-port-option*
     '(:default lilu-swank:*default-swank-port*))
  #.(lilu-roswell:merge-options
     lilu-roswell:*default-swank-host-option*
     '(:default lilu-swank:*default-swank-host*))
  #.lilu-roswell:*default-debugger-option*
  (:name :remote
   :description "remote invocation"
   :required nil
   :default nil
   :short #\r
   :long "remote")
  (:name :quit
   :description "quit swank server"
   :required nil
   :default nil
   :short #\q
   :long "quit"))

(defmacro with-eval ((where key-args) body)
  (let ((where-sym (gensym "WHERE"))
        (key-args-sym (gensym "KEYARGS")))
    `(let ((,where-sym ,where)
           (,key-args-sym ,key-args))
       (if (eql ,where-sym :remote)
           (progn
             (let* ((expr (format nil "~S" ,body)))
               (apply #'lilu-swank:remote-eval-string expr ,key-args-sym)
               ))
           (progn
             (eval ,body))))))

(defun main (&rest argv)
  (declare (ignorable argv))
  #+sbcl (sb-ext:enable-debugger)
  (multiple-value-bind (options free-args)
      (lilu-roswell:parse-options argv
                                  :free-args-p t :min-free-args 1)
    (declare (ignorable free-args))
    (lilu-roswell:options-check-for-help options
                                         :prefix nil
                                         :suffix "commands: list/run"
                                         :usage-of "lilu-launcher.ros"
                                         :args     "COMMAND ARGS")
    (let ((where (if (lilu-roswell:options-get-value options :remote)
                     :remote
                     :local))
          (key-args (lilu-roswell:options-get-keys options :port :host :debugger)))
      (lilu-debugger:with-global-debugger
          ((lilu-roswell:options-get-value options :debugger))
        (lilu-debugger:with-debugger
            ((lilu-roswell:options-get-value options :debugger))
          (let ((command (car free-args))
                (args (cdr free-args)))
            (with-eval (where key-args)
              '(ql:quickload :lilu-launcher))
            (with-eval (where key-args)
              '(lilu-launcher:refresh-applications))
            (cond
              ((equal command "list")
               (format t "~S~%"
                       (with-eval (where key-args)
                         '(mapcar #'lilu-launcher:application-name (lilu-launcher:applications)))))
              ((equal command "run")
               (with-eval (where key-args)
                 `(lilu-launcher:run-app ,(car args))))
              (t
               (error "unkown commad ~A~%" command))))))
      (when (and (lilu-roswell:options-get-value options :remote)
                 (lilu-roswell:options-get-value options :quit))
        (lilu-swank:remote-eval-string "(lilu-roswell:cl-quit)")))))


;;; vim: set ft=lisp lisp:
